package com.skyc.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.skyc.entity.Clothes;
import com.skyc.service.AdminService;
import com.skyc.service.impl.AdminServiceImpl;
import com.skyc.util.FileUp;
import com.skyc.util.RenamePolicyCos;
import com.oreilly.servlet.MultipartRequest;

/**
 * Servlet implementation class AServlet
 */
@WebServlet("/AdminShopShelvesServlet")
public class AdminShopShelvesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest req, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(req, response);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//请求乱码
		response.setContentType("text/html;charset=utf-8");//响应编码
		response.setCharacterEncoding("utf-8");//响应乱码
		PrintWriter out=response.getWriter();
		String op=request.getParameter("op");
		
		if (op.equals("add")) {
		//1.获取文件上传的路径D:\lvse\apache-tomcat-8.5.13\webapps\News\myupload
		   //String savePath=request.getRealPath("/myupload");
		    String savePath="D:\\workspace\\Myeclipse2019\\Git\\项目\\bwj\\00_项目源代码+数据库\\SkyClothes\\WebContent\\images\\temp";//修改为本地地址
		   //2.设置文件上传相关的属性
		   int maxsize=1024*1024*100;   //字节
		   MultipartRequest req=new MultipartRequest(request, savePath, maxsize,"utf-8", new RenamePolicyCos());
		   //开始上传，上传成功返回文件名
		   String filename="";
		try {
			filename = FileUp.uploadfiles(req);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Clothes c = new Clothes();
		c.setCcid(Integer.valueOf(req.getParameter("ccid")));
		c.setCname(req.getParameter("cname"));
		c.setCtype(req.getParameter("ctype"));
		c.setCstock(Integer.valueOf(req.getParameter("cstock")));
		c.setCprice(Integer.valueOf(req.getParameter("cprice")));
		c.setCsales(Integer.valueOf(req.getParameter("csales")));
		c.setCsex(req.getParameter("csex"));
		c.setCcolor(req.getParameter("ccolor"));
		c.setCsize(req.getParameter("csize"));
		c.setCmaintype(req.getParameter("cmaintype"));
		c.setCshopid(req.getParameter("cshopid"));
		String filename1=filename.replaceFirst("/", "");
		c.setCimgurl(filename1);
	    AdminService as=new AdminServiceImpl();
	    String cshopid = req.getParameter("cshopid");
	    boolean falg=as.queryCShopid(cshopid);
	    if(falg){
	    	out.print("<script>alert('商品编号已存在');location='Admin_ShopShelves.jsp';</script>");
		}else{
			boolean falg1=as.save(c);
	 		if(falg1){
	 			out.print("<script>alert('上架衣服成功');location='AdminsellerorderServlet?op=page&page=1';</script>");
	 		}else{
	 			out.print("<script>alert('上架衣服失败');location='Admin_ShopShelves.jsp';</script>");
	 		}
		}
	}
		if (op.equals("del")) {
			AdminService as=new AdminServiceImpl();
			String cshopid = request.getParameter("cshopid");
			boolean falg=as.queryCShopid(cshopid);
		    if(falg){
		    	boolean falg1=as.deteleByShopID(cshopid);
		 		if(falg1){
		 			out.print("<script>alert('下架衣服成功');location='AdminsellerorderServlet?op=page&page=1';</script>");
		 		}else{
		 			out.print("<script>alert('下架衣服失败');location='Admin_ShopShelves2.jsp';</script>");
		 		}
		    }else{
		    	out.print("<script>alert('入库编号不存在');location='Admin_ShopShelves2.jsp';</script>");
			}
		}
		
		else if (op.equals("addStock")) {
			AdminService as=new AdminServiceImpl();
			String cshopid = request.getParameter("cshopid");
			int stock1 = Integer.valueOf(request.getParameter("cstock"));
			boolean falg=as.queryCShopid(cshopid);
			if(falg){
				Clothes c1=as.query(cshopid);
				int stock2=c1.getCstock();
				int stock3=stock2+stock1;
				Clothes c=new Clothes();
				c.setCshopid(cshopid);
				c.setCstock(stock3);
		    	boolean falg1=as.updateStock(c);
		 		if(falg1){
		 			out.print("<script>alert('添加库存成功');location='AdminsellerorderServlet?op=page&page=1';</script>");
		 		}else{
		 			out.print("<script>alert('添加库存失败');location='Admin_ShopShelves3.jsp';</script>");
		 		}
		    }else{
		    	out.print("<script>alert('入库编号不存在');location='Admin_ShopShelves3.jsp';</script>");
			}
		}
		
		else if (op.equals("query")) {
			AdminService as=new AdminServiceImpl();
			String cshopid = request.getParameter("cshopid");
			boolean falg=as.queryCShopid(cshopid);
			if(falg){
				Clothes c=as.query(cshopid);
				int stock=c.getCstock();
				request.setAttribute("cshopid", cshopid);
				request.setAttribute("stock", stock);
				request.getRequestDispatcher("Admin_ShopShelves4.jsp").forward(request, response);
		    }else{
		    	out.print("<script>alert('入库编号不存在');location='Admin_ShopShelves4.jsp';</script>");
			}
		}
		
	out.flush();
	out.close();
	}

}

